<template>
  <div id="timerUpload">
    <el-form :model="timerData" :rules="fromRules" ref="form">
      <el-form-item prop="name">
        <div>
          时间卡片事件名称
          <el-input v-model="timerData.name" type="input" placeholder="请输入卡片事件名称" clearable></el-input>
        </div>
      </el-form-item>
      <el-form-item prop="name">
        <div>
          起始时间：
          <el-date-picker type="date" placeholder="选择起始日期" v-model="timerData.time"></el-date-picker>
        </div>
      </el-form-item>
      <el-form-item prop="finishTime">
        <div>
          结束时间：
          <el-switch v-model="timerData.finishTimeOrNot" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          <el-date-picker style="margin-left: 20px;" type="date" placeholder="选择结束日期"
            v-show="timerData.finishTimeOrNot" v-model="timerData.finishTime"></el-date-picker>
        </div>
      </el-form-item>
      <el-form-item prop="type">
        <div>
          卡片事件类别
          <div class="typeBar">
            <p>1.选择已有的卡片类型</p>
            <el-select v-model="timerData.type">
              <el-option v-for="(item, index) in typeData" :key="index" :value="item" :label="item"></el-option>
            </el-select>
            <p>2.创建新的卡片类型</p>
            <el-input v-model="timerData.type" type="input" placeholder="请输入卡片事件类别" clearable></el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item prop="color">
        <div>
          卡片颜色：<br>
          <el-color-picker v-model="timerData.color" show-alpha :predefine="predefineColors"></el-color-picker>
        </div>
      </el-form-item>
    </el-form>
    <el-button @click="submit" type="success" style="z-index: 20;">提交</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timerData: {
        name: null,
        time: null,
        type: null,
        color: '#f68802',
        finishTimeOrNot: false, // 是否开启结束时间
        finishTime: null,
      },
      typeData: [], // 卡片类型数据
      predefineColors: [
        '#ee820e', '#ea6878', '#299fea', '#ef5a3b', '#2d2d2d'
      ], // 颜色选择器的预定义颜色
      fromRules: {
        name: [
          { required: true, message: '请输入卡片名称', trigger: 'blur' },
        ],
        time: [
          { type: 'date', required: true, message: '请选择起始时间', trigger: 'change' },
        ],
        color: [
          { required: true, message: '请选择卡片颜色', trigger: 'change' },
        ],
        type: [
          { required: true, message: '请选择愿望类型', trigger: 'change' },
          { required: true, message: '请输入愿望类型', trigger: 'blur' },
        ],
        finishTime: [
          { required: false, message: '请选择结束时间', trigger: 'change' },
        ]
      }
    }
  },
  methods: {
    // 上传时间卡片事件
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let data = new FormData()
          data.append("type", this.timerData.type)
          data.append("time", new Date(this.timerData.time))
          data.append("name", this.timerData.name)
          data.append("color", this.timerData.color)
          if(this.timerData.finishTimeOrNot) {
            if(this.timerData.finishTime == null || this.timerData.finishTime == '') {
              this.$message({
                type: 'error',
                message: '请选择结束时间~',
                showClose: true,
              })
              return
            }
            data.append("finishTime",new Date(this.timerData.finishTime))
          }
          // 发送请求
          this.$axios({
            method: 'post',
            url: '/timer/login/uploadTimer',
            headers: {
              'Content-Type': 'application/x-www-form-urlcoded'
            },
            data: data,
          }).then(res => {
            if (res.data) {
              this.$message({
                showClose: true,
                message: '提交成功',
                type: 'success'
              })
            } else {
              this.$message({
                showClose: true,
                message: '提交失败，出现错误了X.X',
                type: 'error'
              })
            }
          })
        } else {
          this.$message({
            type: 'error',
            message: '请完善卡片信息后再提交~',
            showClose: true
          })
        }
      })
    },
    // 获取卡片类型数据
    getAllTimerType() {
      this.$axios({
        method: 'post',
        url: '/timer/login/getAllType'
      }).then(res => {
        this.typeData = res.data
      })
    },
  },
  mounted() {
    this.getAllTimerType()
  }
}
</script>

<style lang="scss" scoped>
#timerUpload {
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.typeBar {
  padding-left: 20px;
}
</style>
<style scoped="scoped" scope>
/deep/.el-input__inner {
  background-color: #202020;
  text-align: center;
  color: #fff;
}

/deep/.el-input__inner {
  width: 300px;
}
</style>